<?php
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\HouseFieldTypeCategory;
use app\portal\model\HouseFieldType;
use think\Db;
use app\admin\model\ThemeModel;

use app\portal\model\HousePassword;

class AdminHousePasswordController extends AdminBaseController
{

    public function default () {
    }

    public function list()
    {
      $param = $this->request->param();
      if (!isset($param['house_id'])) {
        $this->error('非法参数！');
      }
      $house_id = $param['house_id'];
      $list = HousePassword::where('house_id', $house_id)->select();

      $this->assign('list', $list);
      $this->assign('house_id', $house_id);
      return $this->fetch();
    }

    public function addForm () {
      if (!$this->request->isPost()) {
        $this->error('Forbidden', 403);
      }

      $param = $this->request->param();
      if (!isset($param['house_id']) || intval($param['house_id']) == 0) {
        $this->error('非法参数！');
      }
      if (!isset($param['password']) || empty($param['password'])) {
        $this->error('请输入密码！');
      }

      $house_id = $param['house_id'];

      $passwordModel = new HousePassword;
      $passwordModel->house_id = $param['house_id'];
      $passwordModel->password = $param['password'];
      $passwordModel->save();
      $this->success('添加成功');
    }

    public function edit() {
      $param = $this->request->param();

      if (!isset($param['id'])) {
        $this->error('参数不合法！');
      }

      $data = HouseFieldType::find($param['id']);
      $categorys = HouseFieldTypeCategory::all();

      $this->assign('data', $data);
      $this->assign('categorys', $categorys);

      return $this->fetch();
    }

    public function editForm() {
      if (!$this->request->isPost()) {
        $this->error('Forbidden', 403);
      }

      $param = $this->request->param();
      if (!isset($param['id']) || intval($param['id']) == 0) {
        $this->error('非法参数！');
      }
      if (!isset($param['password']) || empty($param['password'])) {
        $this->error('请输入密码！');
      }

      $id = $param['id'];

      $passwordModel = HousePassword::get($id);
      $passwordModel->password = $param['password'];
      $passwordModel->save();
      $this->success('密码修改成功！');
    }

    public function delete() {
      $param = $this->request->param();
      if (!isset($param['id']) || intval($param['id']) == 0) {
        $this->error('非法参数！');
      }

      HousePassword::destroy($param['id']);
      $this->success('删除成功！');
    }

}
